#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Date Date;
typedef struct Family Family;
Family* get_person(void);
void show_people(bool forwards, Family* pfirst, Family* plast);
void release_memory(Family* pfirst);

struct Date{
  int day;
  int month;
  int year;
};

struct Family{
  Date dob;
  char name[20];
  char father[20];
  char monther[20];
  Family* next;
  Family* previous;
};

int main(void){
  Family* first = NULL;
  Family* current = NULL;
  Family* last = NULL;
  char more = '\0';
  while(true){
    printf("\nDo you want to enter details of a%s person (Y or N)?\n",first != NULL?"nother":"");
    getchar();
    scanf("%c",&more);
    if(tolower(more) == 'n'){
      break;
    }
    current = get_person();
    if(first == NULL){
      first = current;
    }else{
      last->next = current;
      current->previous = last;
    }
    last = current;
  }
  
    show_people(true,first,last);
    release_memory(first);
    first = last = NULL;
  return 0;
}

Family* get_person(void){
  Family* temp = (Family*) malloc(sizeof(Family));
  
  printf("\nEnter the name of the person:");
  scanf("%s",temp->name);
  
  printf("\nEnter %s's date of birth (day month year):",temp->name);
  scanf("%d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year);
  
  printf("\nWho is %s's father?",temp->name);
  scanf("%s",temp->father);
  
  printf("\nWho is %s's monther?",temp->name);
  scanf("%s",temp->monther);
  
  temp->next = temp->previous = NULL;
  return temp;
}

void show_people(bool forwards,Family* pfirst,Family* plast){
  printf("\n");
  for(Family* pcurrent = forwards ? pfirst : plast;pcurrent != NULL;pcurrent = forwards ? pcurrent->next:pcurrent->previous){
    printf("%s was born %d/%d/%d and has %s and %s as parents.\n",pcurrent->name,pcurrent->dob.day,pcurrent->dob.month,pcurrent->dob.year,pcurrent->father,pcurrent->monther);
  }
}

void release_memory(Family* pfirst){
  Family* pcurrent = pfirst;
  Family* temp = NULL;
  while(pcurrent){
    temp = pcurrent;
    pcurrent = pcurrent->next;
    free(temp);
  }
}
